package com.woniu.zsd.platform.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.zsd.common.util.JWTUtil;
import com.woniu.zsd.common.util.Result;
import com.woniu.zsd.platform.annotation.RequirePermission;
import com.woniu.zsd.platform.annotation.RequireRole;
import com.woniu.zsd.platform.dto.ResourceDto;
import com.woniu.zsd.platform.form.RoleResourceForm;
import com.woniu.zsd.platform.service.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangtao
 * @since 2025年02月18日
 */
@RestController("platformResourceController")
@RequestMapping("/resource")
@Api(tags = "权限类接口")
public class ResourceController {
    @Resource
    private ResourceService resourceService;

    @Resource
    private JWTUtil jwtUtil;

    //更新权限接口
    @PostMapping("/saveRoleResource")
    @ApiOperation(value = "更新权限接口")
    @RequireRole("超级管理员")
    @RequirePermission("更新权限")
    public Result saveRoleResource(@RequestBody RoleResourceForm form) throws Exception {
        resourceService.saveRoleResource(BeanUtil.toBean(form, RoleResourceForm.class));
        return Result.success("更新权限成功");
    }

    //查看所有权限
    @GetMapping("getAllResource")
    @ApiOperation(value = "查看所有权限资源")
    @RequireRole("超级管理员")
    @RequirePermission("查看所有权限资源")
    public Result getAllResource() throws Exception {
        List<ResourceDto> resources = resourceService.getAllResource();
        return Result.success("获取所有权限资源成功",resources);
    }

    //查看该角色已有的权限资源
    @GetMapping("getRoleResource")
    @ApiOperation(value = "查看该角色已有的权限资源")
    @RequireRole("超级管理员")
    @RequirePermission("查看该角色已有的权限资源")
    public Result getRoleResource(@RequestParam("roleId") Integer roleId) throws Exception {
//        Integer accountId = jwtUtil.getAccountId(token);
        List<com.woniu.zsd.platform.model.Resource> resources = resourceService.getRoleResource(roleId);
        return Result.success("获取角色权限资源成功",resources);
    }


}

